const WxFly = require('../../lib/wxplain.js');

Page({
    data: {
        modalHidden: false,
        score: '0'
    },
    onLoad: function (options) {
      let that = this
        // 页面初始化 options为页面跳转所带来的参数
        wx.getSystemInfo({
          success: function(res) {
            that.setData({
              width: res.screenWidth,
              height: res.screenHeight
            })
          },
        })
    },
    onReady: function () {
        // 页面渲染完成
    },
    startGame: function () {
        const fly = this.fly;
        this.setData({ score: 0, modalHidden: false });
        fly.startGame();
    },
    move: function (event) {
        const fly = this.fly;
        var x = event.touches[0].x;
        var y = event.touches[0].y;
        fly.touchmove(x, y);
    },
    click: function () {
        const fly = this.fly;
        fly.touchclick();
    },
    onShow: function () {
      let that = this
        const fly = this.fly = new WxFly(
            {
                ctx: wx.createContext(),
                id: 'plainId',
                height: that.data.height,
                width: that.data.width,
            });
        fly.on('over', packet => {
            this.setData({ score: packet.score, modalHidden: true });
        });
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    }
})